/*
* Copyright 2016 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-wechat-open-api
*
* @File name : WechatInterfaceApiController.java
*
* @Author : LiuJun
*
* @Date : 2016年12月8日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2016年12月8日    LiuJun    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmcc.gateway.wechat.controller.message;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CookieConstant;
import org.marketing.common.dto.customerInfo.PotentialUserDto;
import org.marketing.common.util.CheckUtil;
import org.marketing.common.util.SerializeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmcc.gateway.wechat.controller.BaseController;
import com.yonyou.gmcc.gateway.wechat.service.interfaces.WechatInterfaceApiServiceInterface;

/**
 *
 * @author LiuJun 微信接口api-controller
 * @date 2016年12月8日
 */
@RestController
@RequestMapping(value = "/wechat/interfaces/api")
public class WechatInterfaceApiController extends BaseController {

	private static Logger logger = Logger.getLogger(WechatInterfaceApiController.class);

	@Autowired
	private WechatInterfaceApiServiceInterface wechatInterfaceApiServiceInterface;

	/**
	 *
	 * @author LiuJun 网页授权获取用户基本信息
	 * @date 2016年12月8日
	 * @param code
	 * @param appid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userInfo", method = RequestMethod.GET, produces = "application/json;UTF-8")
	public PotentialUserDto getUserInfoThroughWebPageAuth(@RequestParam("code") String code,
			@RequestParam("appid") String appid) {
		return wechatInterfaceApiServiceInterface.getUserInfoThroughWebPageAuth(code, appid);
	}

	/**
	 *
	 * @author LiuJun 写入微信浏览器cookies信息
	 * @date 2016年12月8日
	 * @param code
	 * @param appid
	 * @throws Exception
	 */
	@RequestMapping(value = "/cookies", method = RequestMethod.PUT)
	public void writeWechatCookiesInfo(@RequestParam("code") String code, @RequestParam("appid") String appid,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		logger.info("writeWechatCookiesInfo is entry,code=" + code + ";appid=" + appid);
		String openId = getOpenId(request);
		String authorizerAppid = getAuthorizerAppid(request);
		Long potentialUserId = getPotentialUserId(request);
		String dealerCode = getDealerCode(request);
		logger.info("writeWechatCookiesInfo-openid=" + (CheckUtil.NullOrEmpty(openId) ? "" : openId));
		logger.info("writeWechatCookiesInfo-authorizerAppid="
				+ (CheckUtil.NullOrEmpty(authorizerAppid) ? "" : authorizerAppid));
		logger.info("writeWechatCookiesInfo-potentialUserId=" + (potentialUserId == null ? "" : potentialUserId));
		logger.info("writeWechatCookiesInfo-dealerCode=" + (CheckUtil.NullOrEmpty(dealerCode) ? "" : dealerCode));

		if (CheckUtil.NullOrEmpty(getOpenId(request))) {
			PotentialUserDto potentialUserDto = wechatInterfaceApiServiceInterface.getUserInfoThroughWebPageAuth(code,
					appid);
			try {
				logger.info("writeWechatCookiesInfo-potentialUserDto=" + SerializeUtils.serialize(potentialUserDto));
			} catch (Exception e) {
			}
			if (potentialUserDto != null) {
				logger.info("writeWechatCookiesInfo-reponseCookies");
				reponseCookies(CookieConstant.WECHAT_COOKIE_OPENID,
						CheckUtil.NullOrEmpty(potentialUserDto.getDeviceOpenId()) ? ""
								: potentialUserDto.getDeviceOpenId(),
						response);
				reponseCookies(CookieConstant.WECHAT_COOKIE_POTENTIALUSERID,
						(potentialUserDto.getPotentialUserId() == null ? ""
								: potentialUserDto.getPotentialUserId().toString()),
						response);
				reponseCookies(CookieConstant.WECHAT_COOKIE_DEALERCODE,
						CheckUtil.NullOrEmpty(potentialUserDto.getDealerCode()) ? ""
								: potentialUserDto.getDealerCode(),
						response);
				reponseCookies(CookieConstant.WECHAT_COOKIE_DEALER_APPID,
						(CheckUtil.NullOrEmpty(potentialUserDto.getAuthorizerAppid()) ? ""
								: potentialUserDto.getAuthorizerAppid()),
						response);
			}
		}
	}

	/**
	 *
	 * @author LiuJun 菜单发布
	 * @date 2016年12月8日
	 * @param appId
	 * @param accessToken
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/menus", method = RequestMethod.POST, produces = "application/json;UTF-8")
	public @ResponseBody Map<String, Object> publishMenu(@RequestParam("appId") String appId,
			@RequestParam("accessToken") String accessToken) {
		return wechatInterfaceApiServiceInterface.publishMenu(appId, accessToken);
	}

	/**
	 *
	 * @author LiuJun 设置公众号模板类别、保存公众号模板列表
	 * @date 2017年1月12日
	 * @param authorizerAppid
	 * @param authorizerAccessToken
	 * @throws Exception
	 */
	@RequestMapping(value = "/templates", method = RequestMethod.POST)
	public void saveWeChatPublicNumberTemplate(@RequestParam("authorizerAppid") String authorizerAppid,
			@RequestParam("authorizerAccessToken") String authorizerAccessToken) {
		wechatInterfaceApiServiceInterface.saveWeChatPublicNumberTemplate(authorizerAppid, authorizerAccessToken);
	}

}
